#include "StdAfx.h"


//#include "ado.h"
#include "utility.h"

#include "platmanager.h"
//#include "minigeoado.h"
#include ".\rtobject.h"
#include "geohelper.h"

#include "..\osglob.h"

int   CKeyIdColl::Add(int id)
{
	return (int)m_aIds.Add(id);
}

CString CKeyIdColl::FormatSqlIn()
{
	CString sids;
	int nCount=m_aIds.GetSize();
	for (int i=0;i<nCount;i++)
	{
		if (!sids.IsEmpty())
			sids+=",";
		sids+=Convert::NumToStr(m_aIds[i]);
	}
	return sids;
}

int    CKeyIdColl::GetCount()
{
	return (int)m_aIds.GetSize();

}
void    CKeyIdColl::Empty()
{
	m_aIds.RemoveAll();
}

int    CKeyIdColl::GetAt(int index)
{
	return m_aIds.GetAt(index);
}




CGtObject::CGtObject()
{
	m_nObjectID=0;
	m_nKeyID=0;
	m_nFlag=OBJNOCHANGE;
	m_pChunk=NULL;
}

CGtObject::~CGtObject()
{

}


void CGtObject::operator=(const CGtObject& other) 
{
	m_nObjectID=other.m_nObjectID;
	m_nKeyID=other.m_nKeyID;
	m_nFlag=other.m_nFlag;
	m_pChunk=other.m_pChunk;
}


void  CGtObject::ClearEntity(void)
{
	m_pChunk=NULL;
}

CString CGtObjectCol::GetTitle(CGtObject* pObj)
{
	CString s;
	if (pObj)
		s.Format("%d",pObj->m_nKeyID);
	return s;
}


class CompKeyID	
{
public:
	bool operator()(CGtObject *e, CGtObject *arg)
	{
		return e->m_nKeyID < arg->m_nKeyID;
	}
};


class CompObjectID	
{
public:
	int operator()(CGtObject *e, CGtObject *arg)
	{
		return e->m_nObjectID < arg->m_nObjectID;
	}
};


CGtObjectCol::CGtObjectCol()
{
	m_nSortMode=0;
	m_pRelLayer=NULL;
}

CGtObjectCol::~CGtObjectCol()
{
	RemoveAll();
}

CGtObject* CGtObjectCol::FindByKeyID(int nID)
{
	ASSERT (m_nSortMode==1);
	CGtObject pObj; 
	pObj.m_nKeyID=nID;

	std::vector<CGtObject *>::iterator b = m_objs.begin();
	std::vector<CGtObject *>::iterator e = m_objs.end();

	std::vector<CGtObject *>::iterator pos =
		std::lower_bound(b, e, &pObj, CompKeyID());

	if(/*pos != NULL && */pos < e &&  (*pos)->m_nKeyID==nID) 	
		return *pos;

	return NULL;
}


CGtObject* CGtObjectCol::FindByObjectID(int nID,std::vector<CGtObject *>::iterator b,std::vector<CGtObject *>::iterator e)
{
	ASSERT (m_nSortMode==2);

	CGtObject pObj; 

	if (nID<0)
	{
		nID=-nID;
	}
	pObj.m_nObjectID=nID;

	//		std::vector<CGtObject *>::iterator b = m_objs.begin();
	//		std::vector<CGtObject *>::iterator e = m_objs.end();

	std::vector<CGtObject *>::iterator pos =
		std::lower_bound(b, e, &pObj, CompObjectID());

	if(/*pos != NULL && */pos < e &&  (*pos)->m_nObjectID==nID) 	
		return *pos;

	return NULL;
}



CGtObject* CGtObjectCol::FindByObjectID(int nID)
{
	ASSERT (m_nSortMode==2);   //1.7

	CGtObject pObj; 

	if (nID<0)
	{
		nID=-nID;
	}
	pObj.m_nObjectID=nID;

	std::vector<CGtObject *>::iterator b = m_objs.begin();
	std::vector<CGtObject *>::iterator e = m_objs.end();

	std::vector<CGtObject *>::iterator pos =
		std::lower_bound(b, e, &pObj, CompObjectID());

	if(/*pos != NULL && */pos < e &&  (*pos)->m_nObjectID==nID) 	
		return *pos;

	return NULL;
}

BOOL   CGtObjectCol::RemoveByObjectID(int nID)     
{

	ASSERT (m_nSortMode==2);
	CGtObject  pObj; 
	if (nID<0)
	{
		nID=-nID;
	}
	pObj.m_nObjectID=nID;

	std::vector<CGtObject *>::iterator b = m_objs.begin();
	std::vector<CGtObject *>::iterator e = m_objs.end();

	std::vector<CGtObject *>::iterator pos =
		std::lower_bound(b, e, &pObj, CompObjectID());

	if(/*pos != NULL && */pos < e &&  (*pos)->m_nObjectID==nID) 	
	{
		CGtObject*  pDelObj=*pos;
		delete pDelObj;

		m_objs.erase(pos);
		return TRUE ;
	}
	return FALSE;
} 


void   CGtObjectCol::SetByKeyID(CGtObject* pObj)
{
	int nID=pObj->m_nKeyID;

	CGtObject* pFind=FindByKeyID(nID);
	if (pFind!=NULL)
	{
		*pFind=*pObj;
	}
	else
	{
		m_objs.push_back(pObj);
	}
	m_nSortMode=0;
}

void   CGtObjectCol::Add(CGtObject* pObj)
{
	m_objs.push_back(pObj);
	m_nSortMode=0;
}


void   CGtObjectCol::Append(CGtObjectCol& pOtherCol)
{
	std::vector<CGtObject*>::iterator iter;

	for (iter=pOtherCol.m_objs.begin();iter!=pOtherCol.m_objs.end();iter++)
	{
		CGtObject* pObj=*iter;
		m_objs.push_back(pObj);
	}

	pOtherCol.m_objs.clear();

	m_nSortMode=0;
}


void   CGtObjectCol::RemoveAll()
{
	std::vector<CGtObject*>::iterator iter;

	for (iter=m_objs.begin();iter!=m_objs.end();iter++)
	{
		CGtObject* pObj=*iter;
		delete pObj;
	}

	m_objs.clear();
}

void   CGtObjectCol::RemoveDupKeyID()
{
	std::vector<CGtObject*>::iterator iter;
	std::vector<CGtObject*>  objs;

	SortByKeyID();

	long nID=0,nIDBak=0;

	for (iter=m_objs.begin();iter!=m_objs.end();iter++)
	{
		CGtObject* pObj=*iter;
		nID=pObj->m_nKeyID;
		if (nIDBak==nID)
		{
			delete pObj;
		}
		else
		{
			objs.push_back(pObj); 
			nIDBak=nID;
		}
	}

	m_objs.clear();

	for (iter=objs.begin();iter!=objs.end();iter++)
	{
		CGtObject* pObj=*iter;
		m_objs.push_back(pObj); 
	}

	objs.clear();

	m_nSortMode=0;
}


bool CompKeyIDfun(const CGtObject* elem1, const CGtObject* elem2)
{
	return (*elem1).m_nKeyID < (*elem2).m_nKeyID;
}

void CGtObjectCol::SortByKeyID()
{
	m_nSortMode=1;

	std::sort(m_objs.begin(),m_objs.end(),CompKeyID());
}


CGtObject* CGtObjectCol::NewObject(BOOL bAdd)
{
	CGtObject* obj=new CGtObject;
	if (bAdd)
		m_objs.push_back(obj);

	return obj;
}


void CGtObjectCol::SortByObjectID()
{
	m_nSortMode=2;
	std::sort(m_objs.begin(),m_objs.end(),CompObjectID());
}



//IUnknownPtr CGtObjectCol::GetShape(CGtObject* pObj,IGtGeoExchangePtr spGeoExchange,GtGeometryType geoType,VARIANT_BOOL bHasZ,VARIANT_BOOL bHasPointId)
//{
//	CvectorEntityData* pChunk=(CvectorEntityData*)pObj->m_pChunk;
//	return GeoHelper::VtEntityToShape(pChunk,spGeoExchange,geoType,bHasZ,bHasPointId);
//}

//IUnknownPtr CGtObjectCol::GetAnnoElement(CGtObject* pObj,IUnknownPtr spPosShape,IGtGeoExchangePtr spGeoExchange)
//{
//	CvectorEntityData* pChunk=(CvectorEntityData*)pObj->m_pChunk;
//	return GeoHelper::ChunkTextDataToAnnoElement(pChunk,spPosShape,spGeoExchange);
//}


//BOOL   CGtObjectCol::FillAttribToRs(CGtObject *pObj,miniGeoAdo& geoAdo,CGtFields* spEditFlds,GtDataRkMode nRkMode)
//{
//	CvectorEntityData* pChunk=(CvectorEntityData*)pObj->m_pChunk;
//
//	geoAdo.PutValueByChunk(pChunk);
//
//	return TRUE;
//}


//BOOL   CGtObjectCol::GetExtAttribFromRs(CGtObject *pObj,miniGeoAdo& geoAdo,CGtFields* spEditFlds)
//{
//	CvectorEntityData* pChunk=(CvectorEntityData*)pObj->m_pChunk;
//	return TRUE;
//}


//class CompOID	
//{
//public:
//	int operator()(int e, int arg)
//	{
//		return e < arg;
//	}
//};
//
//void COidFinder::AddOid(int nOID)
//{
//
//}
//
//void COidFinder::AddOidFormObjCol(CGtObjectCol* pObjCol)
//{
//	std::vector<CGtObject*>::iterator iter;
//	std::vector<CGtObject*>  objs = pObjCol->m_objs;
//
//	for (iter=objs.begin();iter!=objs.end();iter++)
//	{
//		CGtObject* pObj=*iter;	
//		if (pObj->m_nObjectID<0)
//			m_aOid.push_back(-pObj->m_nObjectID);
//		else
//			m_aOid.push_back(pObj->m_nObjectID);
//	}
//
//	SortOid();
//
//}
//
//void COidFinder::SortOid()
//{
//	std::sort(m_aOid.begin(),m_aOid.end(),CompOID());
//}
//
//
//BOOL COidFinder::FindOId(int nOID)
//{
//
//
//	std::vector<long>::iterator b = m_aOid.begin();
//	std::vector<long>::iterator e = m_aOid.end();
//
//	std::vector<long>::iterator pos =
//		std::lower_bound(b, e, nOID, CompOID());
//
//	if(/*pos != NULL && */pos < e &&  *pos ==nOID)
//		return S_OK;
//
//	return S_FALSE;
//}

